<template>
  <a-modal
    :visible="visible"
    title="修改库存"
    cancelText="取消"
    okText="提交"
    @ok="submit"
    @cancel="cancel"
  >
    <a-form
       ref="formRef"
      :model="formState"
      :rules="formRules"
      :label-col="labelCol"
      :wrapper-col="wrapperCol"
    >
      <a-form-item ref="goodsname" label="物品名称" name="goodsname">
        <a-input v-model:value="formState.goodsname" />
      </a-form-item>
      <a-form-item ref="goodssum" label="物品总数" name="goodssum">
        <a-input v-model:value="formState.goodssum"  disabled="true"/>
      </a-form-item>
      <a-form-item ref="number" label="剩余可用数量" name="number">
        <a-input v-model:value="formState.number"  disabled="true"/>
      </a-form-item>
      <a-form-item label="类型" name="type">
        <a-select v-model:value="formState.type">
          <a-select-option :value="post.value" v-bind:key="index" v-for="(post,index) in state.typeList">{{post.label}}</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="单位" name="unit">
        <a-select v-model:value="formState.unit">
          <a-select-option :value="post.value" v-bind:key="index" v-for="(post,index) in state.unitList">{{post.label}}</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="负责部门" name="departmentcharge">
        <a-select v-model:value="formState.departmentcharge">
          <a-select-option :value="post.id" v-bind:key="index" v-for="(post,index) in state.sysDeptList">{{post.name}}</a-select-option>
        </a-select>
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script>
import { message } from 'ant-design-vue';
import { defineComponent, reactive, ref, toRaw, watch } from "vue";
import {getLMModule,edit} from "@/api/module/laboratory";
export default defineComponent({
  props: {
    visible: {
      type: Boolean,
    },
    record: {
      type: Object,
    }
  },
  emit: ["close"],
  setup(props, context) {

    const formRef = ref();

    const formState = reactive({});

    watch(props, (props) => {
      formState.id = props.record.id
      formState.goodsname = props.record.goodsname
      formState.number = props.record.number
      formState.goodssum = props.record.goodssum
      formState.type = props.record.type
      formState.unit = props.record.unit
      formState.departmentcharge = props.record.departmentcharge
    })

    const formRules = {};
    const state = reactive({
      typeList: [],
      unitList: [],
      sysDeptList: [],
    });

    const editKey = "add";

    const submit = (e) => {
      message.loading({ content: '提交中...', key: editKey });
      formRef.value
        .validate()
        .then(() => {
          edit(toRaw(formState)).then((response)=>{
              if(response.success){
                message.success({ content: '保存成功', key: editKey, duration: 1 }).then(()=>{
                  cancel();
                });
              }else{
                message.success({ content: '保存失败', key: editKey, duration: 1 }).then(()=>{
                  cancel();
                });
              }
          });
        })
        .catch(error => {
          console.log('error', error);
        });
    };

    const cancel = (e) => {
      formRef.value.resetFields();
      context.emit("close", false);
    };
    // 修改查询所有资源模块
    const loadPost = () => {
      getLMModule({}).then((response)=>{
        console.log("response--",response.data)
        state.typeList = response.data.typeList;
        state.unitList = response.data.unitList;
        state.sysDeptList = response.data.sysDeptList;
      })
    }
    /// 加载 资源模块
    loadPost();

    return {

      submit,
      cancel,
      formRef,
      formState,
      formRules,
      state,

      labelCol: { span: 6 },
      wrapperCol: { span: 18 },
    };
  },
});
</script>
